/**
 * Desenvolvido pela Fábrica de Software do CESUPA.
 * Todos os direitos reservados.
 */
package br.cesupa.fabsoft.nomedoprojeto.modules.helpmodule.beans;

import br.cesupa.fabsoft.nomedoprojeto.modules.helpmodule.TreeItem;
import br.cesupa.fabsoft.nomedoprojeto.modules.helpmodule.entities.HelpSet;
import br.cesupa.fabsoft.nomedoprojeto.modules.helpmodule.entities.HelpTopic;
import br.cesupa.fabsoft.nomedoprojeto.modules.helpmodule.services.HelpService;
import java.util.HashSet;
import java.util.Set;

/**
 *
 * @author Breno Leite
 */
public class TreeBuilderBean {

    private HelpService helpService = new HelpService();
    private Set<TreeItem> tableOfContents = new HashSet<TreeItem>();

    public TreeBuilderBean() {
        convert();
    }

    public void convert() {
        HelpSet helpSet = helpService.getTableOfContents();

        TreeItem rootItem = new TreeItem(helpSet.getTitle());
        rootItem.getProperties().put("description", helpSet.getDescription());
        rootItem.getProperties().put("path", "#");
        for(HelpSet set: helpSet.getHelpSets()){
            iterateOverHelpSets(rootItem, set);
        }
        tableOfContents.add(rootItem);
    }

    private void iterateOverHelpSets(TreeItem parentItem, HelpSet helpSet) {
        for (HelpTopic helpTopic : helpSet.getHelpTopics()) {
            TreeItem treeItem = new TreeItem(helpTopic.getTitle());
            treeItem.getProperties().put("description", helpTopic.getDescription());
            treeItem.getProperties().put("keywords", helpTopic.getKeywords());
            treeItem.getProperties().put("path", helpTopic.getPath());
            parentItem.addChild(treeItem);
        }

        for (HelpSet set : helpSet.getHelpSets()) {
            TreeItem treeItem = new TreeItem(helpSet.getTitle());
            treeItem.getProperties().put("description", helpSet.getDescription());
            treeItem.getProperties().put("path", "/#");
            parentItem.addChild(treeItem);
            iterateOverHelpSets(treeItem, set);
        }
    }

    public Set<TreeItem> getTableOfContents() {
        return tableOfContents;
    }
}